package com.chaos.system.controller;

import com.chaos.auth.api.feign.Oauth2Feign;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.dto.result.data.DataResult;
import com.chaos.framework.model.dto.result.list.ListResult;
import com.chaos.framework.model.dto.result.page.PageResult;
import com.chaos.framework.model.helper.ListHelper;
import com.chaos.framework.starter.base.BaseController;
import com.chaos.framework.starter.helper.CheckHelper;
import com.chaos.system.api.data.RbacRoleData;
import com.chaos.system.api.data.RbacUserData;
import com.chaos.system.api.data.RbacUserRoleData;
import com.chaos.system.dto.RbacUserAddDto;
import com.chaos.system.dto.RbacUserRolesDto;
import com.chaos.system.entity.RbacUserRole;
import com.chaos.system.service.RbacUserRoleService;
import com.chaos.system.service.RbacUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Hidden;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@RestController
@Api(tags = "")
@RequestMapping("/rbacUser")
public class RbacUserController extends BaseController {
    @Resource
    private Oauth2Feign oauth2Feign;

    @Resource
    private RbacUserService rbacUserService;
    @Resource
    private RbacUserRoleService rbacUserRoleService;

    @PostMapping("/one.admin")
    @ApiOperation(value = "详情", httpMethod = "POST")
    public DataResult<RbacUserData> one(@RequestBody ID data) {
        return dataResult(rbacUserService.selectByID(data));
    }

    @PostMapping("/list.admin")
    @ApiOperation(value = "列表", httpMethod = "POST")
    public ListResult<RbacUserData> list(@RequestBody RbacUserData data) {
        return listResult(rbacUserService.selectByData(data));
    }

    @PostMapping("/page.admin")
    @ApiOperation(value = "分页", httpMethod = "POST")
    public PageResult<RbacUserData> page(@RequestBody PageQueryDto<RbacUserData> data) {
        return pageResult(rbacUserService.selectByPage(data));
    }

    @PostMapping("/rolePage/page.admin")
    @ApiOperation(value = "分页", httpMethod = "POST")
    public PageResult<RbacUserRolesDto> rolePage(@RequestBody PageQueryDto<RbacUserData> data) {

        PageList<RbacUserRolesDto> page = rbacUserService.rolePage(data);

        List<RbacUserRolesDto> userList = page.getList();
        List<Long> adminIds = ListHelper.getDistinctField(userList, RbacUserRolesDto::getId);
        Map<Long, List<Long>> resourceMap = rbacUserRoleService.selectByAdminIds(adminIds);
        userList.forEach(userInfo -> userInfo.setRoleIds(resourceMap.get(userInfo.getId())));
        return pageResult(page);
    }

    @PostMapping("/add.admin")
    @ApiOperation(value = "新增", httpMethod = "POST")
    public DataResult<ID> add(@RequestBody RbacUserAddDto data) {

        DataResult<String> result = oauth2Feign.passwordEncoder(data.getPassword());
        CheckHelper.requireSuccess(result, "密码加密失败");
        CheckHelper.requireNotBlank(result.getData(), "密码加密失败");
        String password = result.getData();
        RbacUserData userData = RbacUserData.builder()
                .username(data.getUsername()).mobile(data.getMobile()).avatar(data.getAvatar())
                .password(password)
                .status(data.getStatus()).email(data.getEmail()).remark(data.getRemark())
                .build();
        ID adminId = rbacUserService.insertModel(userData);
        List<Long> roleIds = data.getRoles();
        List<RbacUserRole> userRoles = roleIds.stream().map(roleId -> RbacUserRole.builder()
                .userId(adminId.getId()).roleId(roleId)
                .build()).collect(Collectors.toList());
        Boolean isSave = rbacUserRoleService.saveBatch(userRoles);
        return dataResult(adminId);
    }


}

